﻿using ApplicationBase.Core.Logs;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace LogDashboard.Repository.Entities
{
    public class CacheData
    {
        public long Id { get; set; }
        public string Key { get; set; }
        public int HitCount { get; set; }
        public int MissCount { get; set; }
        public double HitRate { get; set; }
        public double AverageElapsed { get; set; }
        public string LogFilePath { get; set; }

        public CacheData(List<CacheLog> logs)
        {
            Key = logs[0].Key;
            foreach (var log in logs)
            {
                if (log.Hit)
                    HitCount++;
                else
                    MissCount++;
            }

            AverageElapsed = logs.Select(it => it.Elapsed).Where(it => it > 0).Average();
            HitRate = (double)HitCount / (MissCount + HitCount);
        }
    }

    public class CacheDataConfig : IEntityTypeConfiguration<CacheData>
    {
        public void Configure(EntityTypeBuilder<CacheData> builder)
        {
            builder.HasKey(it => it.Id);
            builder.Property(it => it.Id).ValueGeneratedNever();
        }
    }
}
